package com.ymt.bpm.dingtalk.web.rest;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.dingtalk.service.DDMAppMgmService;
import com.ymt.bpm.model.PTenantDdMapp;
import com.ymt.bpm.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 微应用配置管理服务
 */
@RestController
@RequestMapping("/api/v1/dingtalk/mappmgm")
public class DDMAppMgmResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private Environment env;

    @Autowired
    private DDMAppMgmService dmams;

    @GetMapping("/default")
    public JSONObject getDefault() {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        return dmams.getDefault(tenantId, loginName, displayName);
    }

    @PostMapping("/save")
    public JSONObject save(@RequestParam("mapp")String mappStr) {
        //Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        //String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("save mapp="+ mappStr);
        }
        return dmams.save(mappStr);
    }

}
